home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / ODF Release 3 / ODFDev / ODF / OS / FWGraphx / FWFontLi.cpp < prev    next >
Encoding:
Text File  |  1996-12-16  |  8.3 KB  |  291 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWFontLi.cpp
  4. //    Release Version:    $ ODF 3 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWFONTLI_H
  13. #include "FWFontLi.h"
  14. #endif
  15.  
  16. // ----- Foundation Includes -----
  17.  
  18. #ifndef FWMEMMGR_H
  19. #include "FWMemMgr.h"
  20. #endif
  21.  
  22. #ifndef FWSTRING_H
  23. #include "FWString.h"
  24. #endif
  25.  
  26. #ifndef _ITEXT_
  27. #include "IText.h"
  28. #endif
  29.  
  30. // ----- Platform Includes -----
  31.  
  32. #ifdef FW_BUILD_WIN
  33. #include <windows.h>
  34. #endif
  35.  
  36. #if defined(FW_BUILD_MAC) && !defined(__MENUS__)
  37. #include <Menus.h>
  38. #endif
  39.  
  40. #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
  41. #include <Script.h>
  42. #endif
  43.  
  44. //========================================================================================
  45. // Runtime infos
  46. //========================================================================================
  47.  
  48. #ifdef FW_BUILD_MAC
  49. #pragma segment FWGraphx_FontList
  50. #endif
  51.  
  52. //========================================================================================
  53. //    Template Instantiations
  54. //========================================================================================
  55.  
  56. #include "FWTColl.tpp"
  57.  
  58. FW_DEFINE_AUTO_TEMPLATE(FW_TOrderedCollectionIterator, FW_CString)
  59. FW_DEFINE_AUTO_TEMPLATE(FW_TOrderedCollection, FW_CString)
  60.  
  61. #ifdef FW_USE_TEMPLATE_PRAGMAS
  62.     #pragma template_access public
  63.     #pragma template FW_TOrderedCollection<FW_CString>
  64.     #pragma template FW_TOrderedCollectionIterator<FW_CString>
  65. #else
  66.     template class FW_TOrderedCollection<FW_CString>;
  67.     template class FW_TOrderedCollectionIterator<FW_CString>;
  68. #endif
  69.  
  70. //========================================================================================
  71. // CLASS FW_CFontList
  72. //========================================================================================
  73.  
  74. FW_DEFINE_AUTO(FW_CFontList)
  75.  
  76. #ifdef FW_BUILD_WIN
  77. //----------------------------------------------------------------------------------------
  78. // PrivWinFillFontList
  79. //
  80. // Windows callback method to insert font names into the supplied list.
  81. //----------------------------------------------------------------------------------------
  82.  
  83. static int CALLBACK PrivWinFillFontList(const LOGFONT*         lplf,
  84.                                         const TEXTMETRIC*    /* lptm */,
  85.                                         DWORD                 /* dwFontType */,
  86.                                         LPARAM                 lParam)
  87. {
  88.     FW_TOrderedCollection<FW_CString>* list = (FW_TOrderedCollection<FW_CString>*) lParam;
  89.     FW_CString* fontName = new FW_CString((FW_Char*) lplf->lfFaceName);
  90.     list->AddLast(fontName);
  91.     return TRUE;        // Keep going
  92. }
  93. #endif
  94.  
  95. //----------------------------------------------------------------------------------------
  96. //    FW_CFontList::FW_CFontList
  97. //----------------------------------------------------------------------------------------
  98.  
  99. FW_CFontList::FW_CFontList()
  100. {
  101. #ifdef FW_BUILD_MAC
  102.     const short dummyMenuID = 7777;
  103.     MenuHandle fontListHolder = ::NewMenu(dummyMenuID, "\pODF");
  104.     
  105.     if (fontListHolder == NULL)
  106.         FW_Failure(FW_xMemoryExhausted);
  107.  
  108.     short count = 0;
  109.  
  110.     FW_TRY
  111.     {
  112.         ::AppendResMenu(fontListHolder, 'FONT');
  113.         count = ::CountMItems(fontListHolder);
  114.         Str255 itemName;
  115.  
  116.         // get System script code and language code
  117.         long systemScriptCode = ::GetScriptManagerVariable(smSysScript);
  118.         long systemLanguageCode = ::GetScriptVariable(systemScriptCode, smScriptLang);
  119.         short itemField;
  120.  
  121.         for (short k = 1; k <= count; k++)
  122.         {
  123.             ::GetMenuItemText(fontListHolder, k, itemName);
  124.             ::GetItemCmd(fontListHolder, k, &itemField);    // Check cmd key equivalent code
  125.  
  126.             ODScriptCode scriptCode = (ODScriptCode) systemScriptCode;
  127.             ODLangCode languageCode = (ODLangCode) systemLanguageCode;
  128.  
  129.             if (itemField == 0x1C)
  130.                     // key equiv value of 0x1C indicates that the icon field contains a script code
  131.             {
  132.                 ::GetItemIcon(fontListHolder, k, &itemField);    // get script code from icon field
  133.                 scriptCode = (ODScriptCode) itemField;
  134.                 if (itemField == 0x00FF)     /* kludge */
  135.                     scriptCode = (ODScriptCode) systemScriptCode;
  136.                 // Get the language code from the script code
  137.                 languageCode = (ODLangCode) ::GetScriptVariable(scriptCode, smScriptLang);
  138.             }
  139.  
  140.             FW_CString* fontName = FW_NEW(FW_CString, ());
  141.             ODIText* text = CreateIText(scriptCode, languageCode, itemName);
  142.             fontName->ReplaceAll(text);
  143.             DisposeIText(text);
  144.             fCollection.AddLast(fontName);
  145.         }
  146.         ::DisposeMenu(fontListHolder);
  147.     }
  148.     FW_CATCH_BEGIN
  149.     FW_CATCH_EVERYTHING()
  150.     {
  151.         ::DisposeMenu(fontListHolder);
  152.         FW_THROW_SAME();
  153.     }
  154.     FW_CATCH_END
  155. #endif
  156.  
  157. #ifdef FW_BUILD_WIN
  158.     HDC dc = ::GetDC(NULL);
  159.     if(dc != 0)
  160.     {
  161.         FW_TRY
  162.         {
  163.             ::EnumFontFamilies(dc, NULL, ::PrivWinFillFontList, (LPARAM)&fCollection);
  164.         }
  165.         FW_CATCH_BEGIN
  166.         FW_CATCH_EVERYTHING()
  167.         {
  168.             ::ReleaseDC(NULL, dc);
  169.             FW_THROW_SAME();
  170.         }
  171.         FW_CATCH_END
  172.         ::ReleaseDC(NULL, dc);
  173.     }
  174. #endif
  175. }
  176.  
  177. //----------------------------------------------------------------------------------------
  178. //    FW_CFontList::~FW_CFontList
  179. //----------------------------------------------------------------------------------------
  180.  
  181. FW_CFontList::~FW_CFontList()
  182. {
  183.     FW_TOrderedCollectionIterator<FW_CString> ite(&fCollection);
  184.     for (FW_CString* fontName = ite.First(); ite.IsNotComplete(); fontName = ite.Next())
  185.     {
  186.         delete fontName;
  187.     }
  188. }
  189.  
  190. //========================================================================================
  191. // CLASS FW_CFontIterator
  192. //========================================================================================
  193.  
  194. FW_DEFINE_AUTO(FW_CFontIterator)
  195.  
  196. //----------------------------------------------------------------------------------------
  197. // FW_CFontIterator::FW_CFontIterator
  198. //----------------------------------------------------------------------------------------
  199.  
  200. FW_CFontIterator::FW_CFontIterator() :
  201.     fFontList(NULL),
  202.     fDeleteTheList(TRUE),
  203.     fIterator(NULL)    
  204. {
  205.     fFontList = FW_NEW(FW_CFontList, ());
  206.     fIterator = FW_NEW(FW_TOrderedCollectionIterator<FW_CString>, (&fFontList->fCollection));
  207.  
  208.     FW_END_CONSTRUCTOR
  209. }
  210.  
  211.  
  212. //----------------------------------------------------------------------------------------
  213. // FW_CFontIterator::FW_CFontIterator
  214. //----------------------------------------------------------------------------------------
  215.  
  216. FW_CFontIterator::FW_CFontIterator(FW_CFontList* fontList) :
  217.     fFontList(fontList),
  218.     fDeleteTheList(FALSE),
  219.     fIterator(NULL)    
  220. {
  221.     FW_ASSERT(fontList != NULL);
  222.     
  223.     fIterator = FW_NEW(FW_TOrderedCollectionIterator<FW_CString>, (&fFontList->fCollection));
  224.  
  225.     FW_END_CONSTRUCTOR
  226. }
  227.  
  228. //----------------------------------------------------------------------------------------
  229. // FW_CFontIterator::~FW_CFontIterator
  230. //----------------------------------------------------------------------------------------
  231.  
  232. FW_CFontIterator::~FW_CFontIterator()
  233. {
  234.     FW_START_DESTRUCTOR
  235.     
  236.     if (fDeleteTheList)
  237.         delete fFontList;
  238.  
  239.     delete fIterator;
  240. }
  241.  
  242. //----------------------------------------------------------------------------------------
  243. // FW_CFontIterator::First
  244. //----------------------------------------------------------------------------------------
  245.  
  246. FW_CString FW_CFontIterator::First()
  247. {
  248.     FW_CString *name = fIterator->First();
  249.     return name ? *name : FW_CString("");
  250. }
  251.  
  252. //----------------------------------------------------------------------------------------
  253. // FW_CFontIterator::Next
  254. //----------------------------------------------------------------------------------------
  255.                             
  256. FW_CString FW_CFontIterator::Next()
  257. {
  258.     FW_CString *name = fIterator->Next();
  259.     return name ? *name : FW_CString("");
  260. }
  261.  
  262. //----------------------------------------------------------------------------------------
  263. // FW_CFontIterator::Last
  264. //----------------------------------------------------------------------------------------
  265.                             
  266. FW_CString FW_CFontIterator::Last()
  267. {
  268.     FW_CString *name = fIterator->Last();
  269.     return name ? *name : FW_CString("");
  270. }
  271.  
  272. //----------------------------------------------------------------------------------------
  273. // FW_CFontIterator::Previous
  274. //----------------------------------------------------------------------------------------
  275.                             
  276. FW_CString FW_CFontIterator::Previous()
  277. {
  278.     FW_CString *name = fIterator->Previous();
  279.     return name ? *name : FW_CString("");
  280. }
  281.  
  282. //----------------------------------------------------------------------------------------
  283. // FW_CFontIterator::IsNotComplete
  284. //----------------------------------------------------------------------------------------
  285.  
  286. FW_Boolean FW_CFontIterator::IsNotComplete()
  287. {
  288.     return fIterator->IsNotComplete();
  289. }
  290.  
  291.